package com.rinlink.gateway.jt808.service;

import com.rinlink.gateway.jt808.jt808.webhook.TerminalDisconnectOutDTO;
import com.rinlink.gateway.jt808.model.TerminalRequestMessage;
import com.rinlink.gateway.jt808.model.NoticeMessageOutDTO;
import com.rinlink.gateway.jt808.model.NoticeMessageType;

public class Jt808GatewayWebHookServiceImpl implements Jt808GatewayWebHookService {

    private String webHookUrl;

    private int timeoutSeconds;

    private WebHookService webHookService;

    public Jt808GatewayWebHookServiceImpl(String webHookUrl, int timeoutSeconds, WebHookService webHookService) {
        this.webHookUrl = webHookUrl;
        this.timeoutSeconds = timeoutSeconds;
        this.webHookService = webHookService;
    }

    public void terminalDisconnect(String terminal){
        TerminalDisconnectOutDTO terminalDisconnectOutDTO =
                new TerminalDisconnectOutDTO()
                        .setTerminalId(terminal)
                        .setTimestamp(System.currentTimeMillis());

        webHookService.push(
                webHookUrl,
                NoticeMessageOutDTO.of(NoticeMessageType.TERMINAL_DISCONNECT,terminalDisconnectOutDTO),
                timeoutSeconds
                );
    }

    @Override
    public void terminalRequest(TerminalRequestMessage message) {
        webHookService.push(
                webHookUrl,
                NoticeMessageOutDTO.of(NoticeMessageType.TERMINAL_REQUEST_MESSAGE,message),
                timeoutSeconds
                );
    }


}
